import React, { Component } from 'react';
import { Button } from 'antd';
import styles from '../../../index.less';
import * as config from '../../../utils/config';

class TabParameterComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sTabId: props.app.currentPane.key, /* 当前标签 */
      componentName: props.componentName, /* 组件名 */
      masterConfig: props.masterConfig, /* 配置 */
      sActiveId: props.sActiveId, /* 窗体Id */
      disabled: props.disabled, /* 是否是查看状态 */
      sId: props.sId, /* 业务数据Id */
      dataList: [],
      paramConfig: props.paramConfig,
    };
  }

  componentWillMount() {
    /* 获取表数据 */
    const {
      masterConfig, sActiveId, sId, sTabId, componentName,
    } = this.state;
    const { dispatch } = this.props;
    const { paramConfig } = this.state;
    /* 主表配置 */
    const paramId = masterConfig.sId;
    /* 表Id */
    const value = {
      sSqlCondition: {
        sParentId: sId,
      },
    };
    const dataUrl = `${config.server_host}business/getBusinessDataByFormcustomId/${paramId}?sModelsId=${sActiveId}`;
    dispatch({
      type: 'content/getUrlPostData',
      payload: {
        url: dataUrl, sTabId, componentName, value, formRoute: '/sisformula', returnType: 'data',
      },
    });
    this.setState({ paramConfig });
  }

  componentWillReceiveProps(nextProps) {
    const { disabled, dataList } = nextProps;
    const {
      sDataListTabId, returnType, loading, componentName,
    } = dataList;
    if (this.state.sTabId === sDataListTabId && this.state.sTabId === nextProps.app.currentPane.key
      && this.state.componentName === componentName) {
      if (loading) return;
      if (returnType === 'data') {
        if (componentName === 'sisformulaparam') {
          this.setState({ disabled });
        }
      }
    }
    this.setState({ disabled, dataList });
  }

  shouldComponentUpdate(nextProps) {
    const { componentName } = nextProps;
    let flag = true;
    if (nextProps.disabled !== this.state.disabled) {
      return true;
    }
    if (this.state.sTabId === nextProps.app.currentPane.key &&
      this.state.componentName === componentName) {
      flag = true;
    } else {
      flag = false;
    }
    return flag;
  }

  handleDoubleClick = (e, child) => {
    this.props.onChangeFormnula(child.sName);
  }

  render() {
    const { dataList, disabled } = this.state;
    return (
      <div className={styles.tabButton}>
        {
          dataList.map(child =>
            (
              <Button
                key={child.sName}
                disabled={disabled}
                onDoubleClick={e => this.handleDoubleClick(e, child)}
              >
                {child.sName}
              </Button>
            ))
        }
      </div>
    );
  }
}

export default TabParameterComponent;
